/*
The purpose of this code is to extract data for Medicare beneficiaries who passed away 
between 2016-2018,to identify ADRD in these individuals, and to identify claims involving
an advanced practice clinician. 

Last updated: 03/14/2023
*/

libname temp 'P:\apced\cms\AIM1\temp';
libname apc 'P:\apced\cms\oneyear';

/*All decedents: 2016-2018*/
proc sql;
	create table decedents as select * from claims.hk100prs_bid	where (year(hkdod)=2016 or year(hkdod)=2017 or year(hkdod)=2018);
	quit;
data temp.decedents_16_18;
	set decedents (keep=bene_id_18900 hkdob hkdod hksex hkrace hkend hknpta hkptas: hkptae: hknptb hkptbs: hkptbe: hknhmo hkhmos: hkhmoe: hkndual hkduals: hkduale: hkdualt: hkcp_alz:)
					;
	run;

/*Build FFS/HMO/Dual indicators*/
data decedents_FFS;
	set temp.decedents_16_18;

	lookup_9mo=intnx('month',hkdod,-9);
	format lookup_9mo date9.;

	array hkptas(15)hkptas: ; array hkptae(15)hkptae: ;
	array hkptbs(15)hkptbs: ; array hkptbe(15)hkptbe: ;
	array hkhmos(12)hkhmos: ; array hkhmoe(12)hkhmoe: ;
	array hkduals(25)hkduals: ; array hkduale(25)hkduale: ;
	array hkdualt(25)hkdualt: ;

	attrib pta_cont_9mo format=8. label="Part A 9mos continuous before death";
	attrib ptb_cont_9mo format=8. label="Part B 9mos continuous before death";
	attrib hmo_any_9mo format=8. label="Any HMO enrollment 9mos before death";
	attrib dual_dod format=8. label="Dual enrollment in month of death";
	attrib dual_9mo format=8. label="Dual enrollment at 9mos before death";

if hknpta>0 then do i=1 to min(hknpta,15);
	if hkptas(i) <= lookup_9mo & hkdod <= hkptae(i) then pta_cont_9mo=1;
		end;
if hknptb>0 then do ii=1 to min(hknptb,15);
	if hkptbs(ii) <= lookup_9mo & hkdod <= hkptbe(ii) then ptb_cont_9mo=1;
		end;
if hknhmo>0 then do iii=1 to min(hknhmo,12);
	if lookup_9mo <= hkhmoe(iii) & hkdod >= hkhmos(iii) then hmo_any_9mo=1;
		end;
if 0<hkndual<=25 then do;
	if ^missing(hkduals(hkndual)) & missing(hkduale(hkndual)) then hkduale(hkdual)=hkend;
		end;
if hkndual>0 then do iv=1 to min(hkndual,25);
	if ((hkduals(iv) <= hkdod & hkdod <= hkduale(iv)) & (hkdualt(iv)=1)) then dual_dod=2;
	if ((hkduals(iv) <= hkdod & hkdod <= hkduale(iv)) & (hkdualt(iv)=2)) then dual_dod=1;
	if ((hkduals(iv) <= lookup_9mo & lookup_9mo <= hkduale(iv)) & (hkdualt(iv)=1)) then dual_9mo=2;
	if ((hkduals(iv) <= lookup_9mo & lookup_9mo <= hkduale(iv)) & (hkdualt(iv)=2)) then dual_9mo=1;
		end;

if pta_cont_9mo=. then pta_cont_9mo=0;
if ptb_cont_9mo=. then ptb_cont_9mo=0;
if hmo_any_9mo=. then hmo_any_9mo=0;
if dual_dod=. then dual_dod=0;
if dual_9mo=. then dual_9mo=0;

if pta_cont_9mo=1 & ptb_cont_9mo=1 & hmo_any_9mo=0 then ffs_full_9mo=1;
	else ffs_full_9mo=0;
run;

data temp.decedents_FFS;
	set decedents_FFS (drop=i ii iii iv hkend hkdual lookup_9mo hknpta hkptas: hkptae: hknptb hkptbs: hkptbe: hknhmo hkhmos: hkhmoe: hkndual hkduals: hkduale: hkdualt:;
	run;

/*Build ADRD indicators*/
data temp.decedents_ADRD;
	set temp.decedents_FFS;
	age=floor((intck('month',hkdob,hkdod)-(day(hkdod) < day(hkdob))) / 12);
	if hkcp_alzdem_fst ne '' then adrd=1;
		else adrd=0;
	run;

/*Identify zip codes*/
proc sql;
	create table zip_2016 as
	select bene_id_18900, hkzip as zip2016
	from claims.hk100ann_bid_2016;
	quit;
proc sql;
	create table zip_2017 as
	select bene_id_18900, hkzip as zip2017
	from claims.hk100ann_bid_2017;
	quit;
proc sql;
	create table zip_2018 as
	select bene_id_18900, hkzip as zip2018
	from claims.hk100ann_bid_2018;
	quit;

proc sql;
	create table zip as select * from zip_2016 as a full join zip_2017 as b
	on a.bene_id_18900 = b.bene_id_18900
	full join zip_2018 as c
	on b.bene_id_18900 = c.bene_id_18900;
	quit;
proc sql;
	create table zip_decedent as select * from temp.decedents_adrd as a left join zip as b
	on a.bene_id_18900 = b.bene_id_18900;
	quit;
data temp.decedents_zip;
	set zip_decedent;
	run;
data temp.decedents (drop=zip2016 zip2017 zip2018);
	set temp.decedents_zip;
	if year(hkdod)=2016 then zip_dod=zip2016;
	if year(hkdod)=2017 then zip_dod=zip2017;
	if year(hkdod)=2018 then zip_dod=zip2018;
	rename hkdob=dob hkdod=dod hkcp_alzdem_fst=adrd_date;
	run;

/*Format, restrict to age 65+ per Shantu*/
proc format library=apc;
	value dual 0="No" 1="Partial" 2="Full";
	value race_apc 0="White, non-Hispanic" 1="Black, non-Hispanic" 2="Hispanic or Latino" 3="Some other race";
	value cohort 1="Community" 2="NH" 3="Transition";
	run;
options fmtsearch=(apc);

data temp.decedents_65 (drop=hkrace hksex);
	retain bene_id_18900 dob dod age female race adrd adrd_date adrd_years zip_dod ffs_full_9mo pta_cont_9mo ptb_cont_9mo hmo_any_9mo dual_dod dual_9mo;
	set temp.decedents;
	if age GE 65;

	if hksex=1 then female=0;
	if hksex=2 then female=1;
	if hkrace=0 then race=.;
	if hkrace=1 then race=0;
	if hkrace=2 then race=1;
	if hkrace=3 then race=3;
	if hkrace=4 then race=3;
	if hkrace=5 then race=2;
	if hkrace=6 then race=3;
	adrd_years=floor((intck('month',adrd_date,dod)-(day(dod) < day(adrd_date))) / 12);
	if adrd_years < 0 then adrd_years=0;

	attrib age				length=3						label='Beneficiary age at death';
	attrib female			length=3	format=yn.			label='Beneficiary is female';
	attrib race				length=3 	format=race_apc.	label='0.White,non-Hispanic 1.Black,non-Hispanic 2.Hispanic/Latino 3.Some other race';
	attrib adrd				length=3	format=yn.			label='ADRD diagnosis (Y/N)';
	attrib adrd_years		length=3						label='Years since initial ADRD diagnosis';
	attrib zip_dod			length=$5	format=$5.			label='ZIP code of beneficiary contact address, year of death';
	attrib ffs_full_9mo		length=3	format=yn.			label='Continuous FFS 9mos before death';
	run;

/*Merge w/ RHF & identify number of episodes*/
proc sql;
	create table temp.decedent_rhf as select * from rhf.V4_all_epb
	where year(hes_dod)=2016 or year(hes_dod)=2017 or year(hes_dod)=2018;
	quit;
proc sql;
	create table decedent_rhf as select * from temp.decedents_65 as a left join temp.decedent_rhf as b
	on a.bene_id_18900 = b.bene_id_18900;
	quit;
data temp.rhf_baseline;
	set decedent_rhf;
	baseline_9mo=intnx('day',dod,-270);
	baseline_1mo=intnx('day',dod,-31);
	format baseline_9mo baseline_1mo date11.;
	length location $20;

	if hee_hospice ne '' then hospice=1;
	if hee_er_count ne . then er_count=hee_er_count;

	if substr(hee_type,1,16)='01. Inp 01:Acute' then location="Acute Inpatient";
	else if substr(hee_type,1,16)='01. Inp 06:CAHos' then location="Acute Inpatient";
	else if substr(hee_type,1,16)='01. Inp 12:LTHos' then location="Chronic Inpatient";
	else if substr(hee_type,1,16)='01. Inp 17:RehHs' then location="Chronic Inpatient";
		else if substr(hee_type,1,7)='02. MDS' then location="Nursing Home";
		else if substr(hee_type,1,7)='01. SNF' then location="Nursing Home";
			else if substr(hee_type,1,7)='03. OBS' then location="Observation";
				else if substr(hee_type,1,7)='04. HHA' then location="Home Health";
					else if substr(hee_type,1,7)='99. Gap' then location="Home";
						else if substr(hee_type,1,7)='99. Une' then location="Unenrolled";
							else location="Other";
	run;

/*Restrict to baseline period (months 9-1 before death)*/
data temp.decedents_rhf_baseline;
	set temp.rhf_baseline;
	where (hee_from LE baseline_9mo LE hee_thru) or (hee_from LE baseline_1mo LE hee_thru) or (baseline_9mo LE hee_from LE baseline_1mo);
/*Overwrite from and thru dates that span the 9mo/1mo dates*/
	if hee_from LE baseline_9mo and hee_thru GE baseline_9mo then do;
     	hee_los = hee_thru - baseline_9mo +1;
		end;
	if hee_from LE baseline_1mo and hee_thru GE baseline_1mo then do;
    	hee_los = baseline_1mo - hee_from +1;
		end;
	if hee_from LE baseline_9mo and hee_thru GE baseline_1mo then do;
		hee_los = baseline_1mo - baseline_9mo +1;
		end;
	if hee_from GE baseline_9mo and hee_thru LE baseline_1mo then do;
		hee_los = hee_thru - hee_from +1;
		end;
	if location = "Acute Inpatient" then los_ip = hee_los;
	if location = "Chronic Inpatient" then los_ltc = hee_los;
	if location = "Nursing Home" then los_nh = hee_los;
	if location = "Observation" then los_obs = hee_los;
	if location = "Home Health" then los_hh = hee_los;
	if location = "Home" then los_home = hee_los;
	if location = "Unenrolled" then los_unen = hee_los;
	if location = "Other" then los_other = hee_los;
	run;  
proc sql;
	create table baseline_summary as select distinct
	bene_id_18900
		,sum(los_ip) as ip_days
		,sum(los_ltc) as ltc_days
		,sum(los_nh) as nh_days
		,sum(los_obs) as obs_days
		,sum(los_hh) as hh_days
		,sum(los_home) as home_days
		,sum(los_unen) as unen_days
		,sum(los_other) as other_days
		,sum(hospice) as hospc_days
		,sum(er_count) as er_count
		from temp.decedents_rhf_baseline
		group by bene_id_18900
		order by bene_id_18900;
	quit;

/*Merge back to base file*/
proc sql;
	create table baseline_merge as select * from temp.decedents_65 as a left join baseline_summary as b
	on a.bene_id_18900 = b.bene_id_18900;
	quit;
data apc.decd_cohort;
	set baseline_merge;
	label ip_days="Baseline # of days in acute hospital";
	label ltc_days="Baseline # of days in long-term hospital";
	label nh_days="Baseline # of days in nursing home";
	label obs_days="Baseline # of days in observation";		
	label hh_days="Baseline # of days in home health";
	label home_days="Baseline # of days in community";
	label hospc_days="Baseline # of days in hospice";
	label unen_days="Baseline # of days unenrolled";
	label other_days="Baseline # of days in other setting";
	label cohort="Baseline cohort: 1.Community 2.Nursing Home 3.Transition";
	format cohort cohort.;
	
	if ip_days=. then ip_days=0;
	if ltc_days=. then ltc_days=0;
	if nh_days=. then nh_days=0;
	if obs_days=. then obs_days=0;
	if hh_days=. then hh_days=0;
	if home_days=. then home_days=0;
	if hospc_days=. then hospc_days=0;
	if unen_days=. then unen_days=0;
	if other_days=. then other_days=0;

	acute_days = ip_days + obs_days;
	acute_days_er = acute_days + er_count;

	if nh_days=0 then cohort=1;
		else if home_days=0 & hh_days=0 then cohort=2;
		else cohort=3;
	run;
data apc.adrd_cohort;
	set apc.decd_cohort;
	where ffs_full_9mo=1 & adrd=1;
	run;

/*Analytic cohort (for merges)*/
data apc.adrd_cohort_small (keep=bene_id_18900 cohort dod baseline_9mo baseline_1mo);
	set apc.adrd_cohort;
	baseline_9mo=intnx('day',dod,-270);
	baseline_1mo=intnx('day',dod,-31);
	format baseline_9mo baseline_1mo date11.;
	run;

/*Nursing home RHF file (ADRD cohort)*/
data temp.adrd_nh_rhf (keep=bene_id_18900 hkdod baseline_9mo baseline_1mo hee_from hee_thru hee_provn hee_type location hee_los);
	set full.decedents_rhf_baseline;
	where (year(hkdod) in(2016,2017,2018)) & ffs_full_9mo=1 & adrd=1 & location="Nursing Home";
	run;

/*Carrier data pull*/
data temp.all_lines_new;
	set claims.hbapcline (keep=bene_id_18900 hbclmid hbthru hbfrom hbbetos hbprvtp hbhspec hbplcsrv hbpfnpi hbtaxnum);
	where hbhspec in('01','08','11','12','17','38','50','84','97') and hbbetos in('M1A','M1B','M4A','M4B');
	if hbhspec='50' then provider=1;	/*NP*/
	if hbhspec='97' then provider=2;	/*PA*/
	if hbhspec in('1','01','8','08','11','12','17','38','84') then provider=0;	/*MD*/
	run;
proc sort data=temp.all_lines_new;
	by bene_id_18900 hbthru hbfrom;
	run;
